﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Data.Common;
using System.Drawing;

namespace YourMoney
{
    class StaticMembers
    { 
        
        /// <summary>
        ///  Задает дерево видов финансовых операаций, на вход подаётся корневой элемент, остальные дочерние берутся из базы данных...
        /// </summary>
        /// <param name="root"></param>
        public static void SetEssenceTree(TreeNode root)
        {
            if (root != null)
            {
                SQLiteDriver drv = SQLiteDriver.GetDrv("Money.db3");

                SQLiteConnection conn = drv.Connection;
                SQLiteCommand commandParam = new SQLiteCommand(conn);
                commandParam.Parameters.Add(new SQLiteParameter("@parent_id", ((TypeOfEssence)root.Tag).ID));
                commandParam.CommandText = "select * from TypeOfEssence where parent_id = @parent_id;";
                foreach (DbDataRecord record in commandParam.ExecuteReader())
                {
                    int isRoot = Convert.ToInt32(record["is_root"]);
                    int id = Convert.ToInt32(record["id"]);
                    int parentId = Convert.ToInt32(record["parent_id"]);
                    string name = record["name"].ToString();
                    int kindId = Convert.ToInt32(record["root_id"]);
                    int color = Convert.ToInt32(record["color"]);
                    TypeOfEssence TofEss = (new TypeOfEssence(id, parentId, kindId, isRoot, name, color));
                    TreeNode node = new TreeNode(TofEss.ToString());
                    node.Tag = TofEss;
                    node.BackColor = ControlPaint.Light(Color.FromArgb(color),3/2);
                    root.Nodes.Add(node);
                    SetEssenceTree(node);
                }
            }
        }
        /// <summary>
        /// выбирает из базы корневые элементы (у которых is_root=1)
        /// </summary>
        /// <returns></returns>
        public static List<TreeNode> RootEssenceSelect()
        {
            SQLiteDriver drv = SQLiteDriver.GetDrv("Money.db3");
            string command = "select * from TypeOfEssence where is_root=1;";
            List<TreeNode> arr = new List<TreeNode>();
            foreach (DbDataRecord record in drv.Query(command))
            {
                int isRoot = Convert.ToInt32(record["is_root"]);
                int id = Convert.ToInt32(record["id"]);
                int parentId = Convert.ToInt32(record["parent_id"]);
                string name = record["name"].ToString();
                int kindId = Convert.ToInt32(record["root_id"]);
                int color = Convert.ToInt32(record["color"]);
                TypeOfEssence TofEss = (new TypeOfEssence(id, parentId, kindId, isRoot, name, color));
                TreeNode node = new TreeNode(TofEss.ToString());
                node.Tag = TofEss;
                node.BackColor = Color.FromArgb(color);
                arr.Add(node);
            }
            return arr;

        }
        /// <summary>
        /// выбирает из базы виды фин.операций (доход/расход) для KindCheckBox'ов
        /// </summary>
        /// <returns></returns>
        public static List<TypeOfEssence> KindSelect()
        {
            SQLiteDriver drv = SQLiteDriver.GetDrv("Money.db3");
            List<TypeOfEssence> list = new List<TypeOfEssence>();
            var command = "SELECT * FROM TypeOfEssence where is_root=1;";
            var reader = drv.Query(command);
            foreach (DbDataRecord record in reader)
            {
                int id = Convert.ToInt32(record["id"]);
                string name = (record["name"]).ToString();
                list.Add(new TypeOfEssence(id, name));

            }
            return list;
        }

    }
}
